package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.service.WmNewsMaterialService;
import heima.file.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private WmNewsMaterialService wmNewsMaterialService;

    /**
     * 图片上传
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //1.检查参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.上传图片到minIO中
        String fileName = UUID.randomUUID().toString().replace("-", "");
        //aa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId = null;
        try {
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}", fileId);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3.保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        //wmMaterial.setUserId(1102);
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);

        //4.返回结果

        return ResponseResult.okResult(wmMaterial);
    }

    //图片列表展示
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        dto.checkParam();
        //构造分页器
        Page page = new Page(dto.getPage(), dto.getSize());
        QueryWrapper<WmMaterial> queryWrapper = new QueryWrapper<>();
        //判断是否进行了收藏
        if (dto.getIsCollection() == 1) {
            queryWrapper.eq("is_collection", dto.getIsCollection());
        }
        //根据id查询用户信息
        queryWrapper.eq("user_id", WmThreadLocalUtil.getUser().getId());

        page = page(page, queryWrapper);
        //封装返回jeiguo
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /*
     * @description: 删除图片
     * @author: 椎名真白
     * @date: 2022-06-14 19:21
     * @param: id
     * @return: com.heima.model.common.dtos.ResponseResult
     **/
    @Override
    public ResponseResult delete(Integer id) {
        //判断图片是否被引用
        QueryWrapper<WmNewsMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_id", id);
        int count = wmNewsMaterialService.count(queryWrapper);
        //Map<String, Object> map = wmNewsMaterialService.getMap(queryWrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.HAVE_RELATE);
        }
        //进行删除
        this.removeById(id);
        return ResponseResult.okResult(200, "删除成功");
    }

    /*
     * @description: 取消收藏
     * @author: 椎名真白
     * @date: 2022-06-14 19:21
     * @param: id
     * @return: com.heima.model.common.dtos.ResponseResult
     **/
    @Override
    public ResponseResult cancelCollection(Integer id) {
        QueryWrapper<WmMaterial> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        WmMaterial wmMaterial = this.getOne(queryWrapper);
        wmMaterial.setIsCollection((short)0);
        this.updateById(wmMaterial);
        return ResponseResult.okResult(200,"取消收藏成功");
    }

    /*
     * @description: 收藏
     * @author: 椎名真白
     * @date: 2022-06-14 19:27
     * @param: id
     * @return: com.heima.model.common.dtos.ResponseResult
     **/
    @Override
    public ResponseResult collection(Integer id) {
        QueryWrapper<WmMaterial> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        WmMaterial wmMaterial = this.getOne(queryWrapper);
        wmMaterial.setIsCollection((short)1);
        this.updateById(wmMaterial);
        return ResponseResult.okResult(200,"收藏成功");
    }

}